package it.polimi.entities;

import java.io.Serializable;

import javax.persistence.*;

import java.util.Date;
import java.util.List;

import it.polimi.authentication.ejb.*;
import it.polimi.authentication.ejb.usermanagement.User;
import it.polimi.entitiesDTO.PersonalizedPacketDTO;

/**
 * The persistent class for the personalized_packet database table.
 * 
 */
@Entity
@Table(name="personalized_packet")
@NamedQueries ({ @NamedQuery(name=PersonalizedPacket.FIND_ALL, query="SELECT p FROM PersonalizedPacket p"),
	 			 @NamedQuery(name=PersonalizedPacket.FIND_PACKET, query="SELECT p FROM PersonalizedPacket p WHERE p.packet = :packet"),
	 			 @NamedQuery(name=PersonalizedPacket.FIND_BY_USER, query="SELECT p FROM PersonalizedPacket p WHERE p.user1 = :user1"),
	 			 @NamedQuery(name=PersonalizedPacket.FIND_BY_EMPLOYEE, query="SELECT p FROM PersonalizedPacket p WHERE p.user2 = :user2")

})
@NamedQuery(name="PersonalizedPacket.findAll", query="SELECT p FROM PersonalizedPacket p")
public class PersonalizedPacket implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static final String FIND_ALL = "PersonalizedPacket.findAll";
	
	public static final String FIND_PACKET = "PersonalizedPacket.findPacket";
	
	public static final String FIND_BY_USER = "PersonalizedPacket.findByUser";
	
	public static final String FIND_BY_EMPLOYEE = "PersonalizedPacket.findByEmployee";

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int packet;

	private String city;

	private boolean confirmed;

	@Lob
	private String description;

	@Temporal(TemporalType.DATE)
	private Date end;

	private int partecipants;

	private int price;

	private int spots;

	@Temporal(TemporalType.DATE)
	private Date start;
	
	@Temporal(TemporalType.DATE)
	private Date departure;
	
	
	@Temporal(TemporalType.DATE)
	@Column(name="`RETURN`")
	private Date return_;
	
	//bi-directional many-to-one association to PacketAttraction
	@OneToMany(mappedBy="personalizedPacket")
	private List<PacketAttraction> packetAttractions;

	//bi-directional many-to-one association to PacketFlight
	@OneToMany(mappedBy="personalizedPacket")
	private List<PacketFlight> packetFlights;

	//bi-directional many-to-one association to Hotel
	@ManyToOne
	@JoinColumn(name="hotel")
	private Hotel hotel;

	//bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name="user")
	private User user1;

	//bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name="employee")
	private User user2;

	public PersonalizedPacket() {
	super();
	}
	
	public PersonalizedPacket(PersonalizedPacketDTO packetDTO, Packet packet, User user, User employee){
		this.city = packet.getCity();
		this.description = packet.getDescription();
		this.confirmed = false;
		this.start = packet.getStart();
		this.end = packet.getEnd();
		this.hotel = packet.getHotel();
		this.price = packet.getPrice();
		this.partecipants = packetDTO.getPartecipants();
		this.user1 = user;
		this.user2 = employee;
	}
	
	public PersonalizedPacket (PersonalizedPacket packet, User user, User employee){
		this.city = packet.getCity();
		this.description = packet.getDescription();
		this.confirmed = false;
		this.start = packet.getStart();
		this.end = packet.getEnd();
		this.hotel = packet.getHotel();
		this.price = packet.getPrice();
		this.user1 = user;
		this.user2 = employee;
	}
	
	public int getPacket() {
		return this.packet;
	}

	public void setPacket(int packet) {
		this.packet = packet;
	}

	public String getCity() {
		return this.city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public boolean getConfirmed() {
		return this.confirmed;
	}

	public void setConfirmed(boolean confirmed) {
		this.confirmed = confirmed;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getEnd() {
		return this.end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public int getPartecipants() {
		return this.partecipants;
	}

	public void setPartecipants(int partecipants) {
		this.partecipants = partecipants;
	}

	public Date getDeparture() {
		return departure;
	}

	public void setDeparture(Date departure) {
		this.departure = departure;
	}

	public Date getReturn_() {
		return return_;
	}

	public void setReturn_(Date return_) {
		this.return_ = return_;
	}

	public int getPrice() {
		return this.price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getSpots() {
		return this.spots;
	}

	public void setSpots(int spots) {
		this.spots = spots;
	}

	public Date getStart() {
		return this.start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Hotel getHotel() {
		return this.hotel;
	}

	public void setHotel(Hotel hotel) {
		this.hotel = hotel;
	}

	public User getUser1() {
		return this.user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}

	public User getUser2() {
		return this.user2;
	}

	public void setUser2(User user2) {
		this.user2 = user2;
	}
	
	public List<PacketAttraction> getPacketAttractions() {
		return this.packetAttractions;
	}

	public void setPacketAttractions(List<PacketAttraction> packetAttractions) {
		this.packetAttractions = packetAttractions;
	}

	public PacketAttraction addPacketAttraction(PacketAttraction packetAttraction) {
		getPacketAttractions().add(packetAttraction);
		packetAttraction.setPersonalizedPacket(this);

		return packetAttraction;
	}

	public PacketAttraction removePacketAttraction(PacketAttraction packetAttraction) {
		getPacketAttractions().remove(packetAttraction);
		packetAttraction.setPersonalizedPacket(null);

		return packetAttraction;
	}

	public List<PacketFlight> getPacketFlights() {
		return this.packetFlights;
	}

	public void setPacketFlights(List<PacketFlight> packetFlights) {
		this.packetFlights = packetFlights;
	}

	public PacketFlight addPacketFlight(PacketFlight packetFlight) {
		getPacketFlights().add(packetFlight);
		packetFlight.setPersonalizedPacket(this);

		return packetFlight;
	}

	public PacketFlight removePacketFlight(PacketFlight packetFlight) {
		getPacketFlights().remove(packetFlight);
		packetFlight.setPersonalizedPacket(null);

		return packetFlight;
	}
	
	public String toString(){
		return "Id: "+this.getPacket() + " city: "+ this.getCity() + " Description: " +this.getDescription() + " Hotel Name: " + this.getHotel().getName() + "can join from" + this.getStart().toString() /* + " To " + this.getEnd().toString() + " Departure " + this.getDeparture().toString() + "Return " + this.getReturn_().toString()*/ + "Partecipants " + this.getPartecipants() + " Open Spots" + this.getSpots() + "Price " + this.getPrice() + "is confirmed? " + this.getConfirmed();
	}

}